﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace AfterlifeLib.Input
{
    /// <summary>
    /// Class voor input met high-level functies.
    /// </summary>
    public static class InputState
    {
        public static KeyboardState curKeyboardState;
        public static KeyboardState prevKeyboardState;

        public static MouseState curMouseState;
        public static MouseState prevMouseState;

        public static int MouseX
        {
            get { return curMouseState.X; }
        }

        public static int MouseY
        {
            get { return curMouseState.Y; }
        }

        public static Vector2 MousePos
        {
            get { return new Vector2(MouseX, MouseY); }
        }

        /// <summary>
        /// Hoeveel de X coordinaat van de muis heeft bewogen sinds de vorige
        /// MouseState.
        /// </summary>
        public static int MouseDeltaX
        {
            get { return curMouseState.X - prevMouseState.X; }
        }

        /// <summary>
        /// Hoeveel de Y coordinaat van de muis heeft bewogen sinds de vorige
        /// MouseState.
        /// </summary>
        public static int MouseDeltaY
        {
            get { return curMouseState.Y - prevMouseState.Y; }
        }

        public static bool Up
        {
            get { return IsKeyFirstDown(Keys.Up); }
        }

        public static bool Down
        {
            get { return IsKeyFirstDown(Keys.Down); }
        }

        public static bool Left
        {
            get { return IsKeyFirstDown(Keys.Left); }
        }

        public static bool Right
        {
            get { return IsKeyFirstDown(Keys.Right); }
        }

        public static bool Accept
        {
            get { return IsKeyFirstDown(Keys.Space) || IsKeyFirstDown(Keys.Enter); }
        }

        public static bool Cancel
        {
            get { return IsKeyFirstDown(Keys.Escape); }
        }

        /// <summary>
        /// Update alle states.
        /// </summary>
        public static void Update()
        {
            prevKeyboardState = curKeyboardState;
            prevMouseState = curMouseState;

            curKeyboardState = Keyboard.GetState();
            curMouseState = Mouse.GetState();
        }

        /// <summary>
        /// Of een toets is ingedrukt.
        /// </summary>
        /// <param name="key">Welke toets.</param>
        /// <returns>Wel of niet ingedrukt.</returns>
        public static bool IsKeyDown(Keys key)
        {
            if (curKeyboardState.IsKeyDown(key))
                return true;
            
            return false;
        }

        public static bool IsKeyUp(Keys key)
        {
            if(curKeyboardState.IsKeyUp(key))
                return true;

            return false;
        }

        public static bool IsKeyFirstDown(Keys key)
        {
            if (prevKeyboardState.IsKeyUp(key) && curKeyboardState.IsKeyDown(key))
                return true;

            return false;
        }

        public static bool IsKeyFirstUp(Keys key)
        {
            if (prevKeyboardState.IsKeyDown(key) && curKeyboardState.IsKeyUp(key))
                return true;

            return false;
        }

        #region Down
        public static bool IsLeftButtonDown()
        {
            return curMouseState.LeftButton == ButtonState.Pressed;
        }

        public static bool IsMiddleButtonDown()
        {
            return curMouseState.MiddleButton == ButtonState.Pressed;
        }

        public static bool IsRightButtonDown()
        {
            return curMouseState.RightButton == ButtonState.Pressed;
        }
        #endregion

        #region Up
        public static bool IsLeftButtonUp()
        {
            return curMouseState.LeftButton == ButtonState.Released;
        }

        public static bool IsMiddleButtonUp()
        {
            return curMouseState.MiddleButton == ButtonState.Released;
        }

        public static bool IsRightButtonUp()
        {
            return curMouseState.RightButton == ButtonState.Released;
        }
        #endregion

        #region FirstDown
        public static bool IsLeftButtonFirstDown()
        {
            return (prevMouseState.LeftButton == ButtonState.Released && 
                curMouseState.LeftButton == ButtonState.Pressed);
        }
        #endregion
    }
}
